<?php

namespace MLM\Controller;

use Zend\View\Model\ViewModel;
use Zend\Authentication\Result;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;

class SnController extends \MLM\Model\Generic\Controller
{
        
    public function indexAction()
    {

        $identity = $this->hasSession();
        $this->layout('layout/admin');

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        
        return new ViewModel();

    }
    


    public function generateAction()
    {

        $identity = $this->hasSession();
        $this->layout('layout/admin');

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        
        $view = new ViewModel();

        $script = $this->getServiceLocator()->get('viewhelpermanager')
                    ->get('inlineScript');
            $script->appendScript("
               $('#num').ace_spinner({value:10,min:1,max:9999,step:10, btn_up_class:'btn-info' , btn_down_class:'btn-info'})
                .on('change', function(){
                        //alert(this.value)
                });
            ");

        //$view->setTerminal(true);
        return $view;

    }

    public function cetakAction()
    {

        $identity = $this->hasSession();
        $this->layout('layout/admin');

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        return new ViewModel();


    }

    public function aktifasiAction()
    {
        $identity = $this->hasSession();
        $this->layout('layout/admin');
        $view = new ViewModel();

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        $request = $this->getRequest();

        $db = $this->_db;
        $conn = $db->getDriver()->getConnection();

        $script = $this->getServiceLocator()->get('viewhelpermanager')
                ->get('inlineScript');
        $script->appendScript("
          jQuery(function($) {
            $('.chosen-select').chosen();

            $('#modal-form').on('shown.bs.modal', function() {
                $(this).find('.chosen-container').each(function() {
                    $(this).find('a:first-child').css('width', '210px');
                    $(this).find('.chosen-drop').css('width', '210px');
                    $(this).find('.chosen-search input').css('width', '200px');
                });
            })
        })
        ");

        if ($request->isPost()) {
            $conn->beginTransaction();
            $formData = $request->getPost();

            $sn   = $formData['sn'];
            $snList   = $formData['snList'];
            $flag   = $formData['flag'];
            
            $err = 0;
            $errorMsg = "";

            /*
             * Check List
             */
            if ($snList == "" && $flag == 1){
                $errorMsg .= "* SN harus dipilih.<br>";
                //return;
                $err = 1;
            }


            $msg = "";
            if ($err == 0){

                    if ($flag == 1){ //Aktifkan yang dipilih
                        $msg = "Diaktifkan";
                    }else if ($flag == 2){ //Aktifkan semua
                        $msg = "Diaktifkan Semua";
                    }else if ($flag == 3){  //Pasifkan semua
                        $msg = "Dipasifkan Semua";
                    }

                    $storage = new \MLM\Model\Sn\Storage($db);
                    $res = $storage->aktivasiSN($snList, $flag, $sn);

                    if(!$res){
                        $conn->rollback();
                        $view->setVariable('msg', 'SN Gagal '.$msg);
                        return $view;
                    }

                    $conn->commit();
                    $view->setVariable('msg', 'SN Berhasil '.$msg);
                    return $view;

            }else{
              
                $view->setVariable('errmsg', $errorMsg);
                return $view;
            }



        }else{
            
            return $view;
        }

       
    }


   //Get Messages Notif
    private function getMsgNotif($memberTypeId, $memberId){

        $notifStorage = new \MLM\Model\Notification\Storage($this->_db);
        $notifMsg = new \MLM\Model\Notification($notifStorage);
        $newMsgCnt = $notifMsg->getCountNewMsg($memberId);
        $newMsgList = $notifMsg->getNewMsgList($memberId);
        $newAlertCnt = $notifMsg->getCountAlertApproval($memberTypeId, $memberId);
        $newAlertList = $notifMsg->getAlertApproval($memberTypeId, $memberId);

        $action = $this->getEvent()->getRouteMatch()->getParam('action');
        $control = $this->getEvent()->getRouteMatch()->getParam('controller');

        $this->layout()->menu = $control.'\\'.$action;
        $this->layout()->newMsgCnt = $newMsgCnt;
        $this->layout()->newMsgList = $newMsgList;
        $this->layout()->newAlertCnt = $newAlertCnt;
        $this->layout()->newAlertList = $newAlertList;
        
    }


   private function hasSession() {
        $auth = new AuthenticationService();

        $identity = $auth->getIdentity();

        if (!$auth->hasIdentity() || $identity->member_type_id <> 4) {
            //return $this->redirect()->toRoute('admin', array('controller' => 'Admin', 'action' => 'login'));
            return $this->redirect()->toRoute('application/default', array('controller' => 'Index', 'action' => 'login'));
        } else {
            return $auth->getIdentity();
        }
    }

}

